package com.atguigu.edu.service.impl;

import com.atguigu.edu.client.VodClientService;
import com.atguigu.edu.service.VideoService;
import com.atguigu.edu.entity.Video;
import com.atguigu.edu.mapper.VideoMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author ld
 * @since 2022-08-16
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {
    @Resource
    private VodClientService vodClientService;
    @Override
    public void removeVideoByCourseId(String courseId) {
        LambdaQueryWrapper<Video> queryWrapperVideoId=new LambdaQueryWrapper<>();
        queryWrapperVideoId.eq(Video::getCourseId,courseId)
                .select(Video::getVideoSourceId);
        List<Video> videoList = baseMapper.selectList(queryWrapperVideoId);
        List<String> videoIdList =new ArrayList<>();
        for (Video video:videoList){
            String videoSourceId = video.getVideoSourceId();
            if(!StringUtils.isEmpty(videoSourceId)){
                videoIdList.add(videoSourceId);
            }
        }
        vodClientService.deleteBatchVideo(videoIdList);
        LambdaQueryWrapper<Video> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(courseId),Video::getCourseId,courseId);
        baseMapper.delete(queryWrapper);
    }
}
